/**
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import prompt from '@system.prompt';
import wifi from '@ohos.wifi_native_js';


export default {
    data: {
        pwdValue: '',
        isDisabled: true,
        isConn: false
    },
    props: {
        wifiName: ''
    },
    onInit() {
        this.$watch('isConn', 'onConnChange');
    },
    onConnChange() {
        this.$emit('eventType', {
            message: '已连接-' + this.wifiName
        });
    },
    cancelSchedule() {
        this.$element('wifi-dialog').close();
        this.pwdValue = '';
        this.isDisabled = true;
        this.isConn = false;
    },
    inputChange(e) {
        this.pwdValue = e.value;
    },
//点击连接
    setSchedule() {
        if (this.pwdValue == '') { //密码不能为空
            this.$element('input').showError({
                error: this.$t('strings.wifi-pass')
            });
            return
        } else if (this.pwdValue.length < 8) { //密码不能小于8位
            this.$element('input').showError({
                error: this.$t('strings.wifi-pass-details')
            });
            return
        }
        prompt.showToast({
            message: this.$t('strings.connecting') + this.wifiName
        })
        //连接wifi
        this.connectWifi(this.wifiName, this.pwdValue);
        this.$element('wifi-dialog').close();
    },
    clickChangeChart(num) {
        if (num == 11) {
            if (this.pwdValue.length > 0) {
                this.pwdValue = this.pwdValue.substring(0, this.pwdValue.length - 1);
                this.isDisabled = this.pwdValue.length < 8;
            }
            return
        }
        if (num == 12) {
            this.pwdValue = '';
            this.isDisabled = this.pwdValue.length < 8;
            return
        }
        this.pwdValue += num;
        this.isDisabled = this.pwdValue.length < 8;
    },
//连接指定wifi
    connectWifi(wifiName, wifiPass) {
        let config = {
            'ssid': wifiName,
            'bssid': '',
            'preSharedKey': wifiPass,
            'isHiddenSsid': false,
            'securityType': 3
        }
        this.isConn = wifi.connectToDevice(config);
    }
}
 
 